package com.aiyige.page.my.order.orderDetail;

import android.text.TextUtils;

import com.aiyige.MyApp;
import com.aiyige.R;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.AgreementBackup;
import com.aiyige.model.Subject;
import com.aiyige.model.eshop.BuyerBackup;
import com.aiyige.model.eshop.Order;
import com.aiyige.model.eshop.OrderPay;
import com.aiyige.model.eshop.ProductsBackup;
import com.aiyige.model.eshop.SellerBackup;
import com.aiyige.model.moment.entity.MajorCourse;
import com.aiyige.model.moment.entity.TrainingCourse;
import com.aiyige.utils.ListUtil;

import java.util.List;

/**
 * 用于展示页面的model data
 * Created by xiachao on 2018/2/6.
 */

public class OrderDetailModelData {


    private String userName;
    private String mobile;


    public String getBuyerUserId() {
        if (buyerBackup != null) {
            buyerUserId = buyerBackup.getId();
        }
        return buyerUserId;
    }

    public void setBuyerUserId(String buyerUserId) {
        this.buyerUserId = buyerUserId;
    }

    private String buyerUserId;
    private String buyerUserName;
    private String buyerMobile;
    private String code;
    private String goodsType;
    private int status;
    private String cover;
    private String title;
    private long createTime;
    private long payTime;
    private int payMethod;
    private String payCode;
    private String sellerName;
    private String sellerId;
    private int sellerLevel;
    private String sellerAvatar;
    private String buyerAvatar;
    private double amount;
    private double payAmount;
    private int videoStages;
    private String source;
    private String sourceUrl;
    private String router;
    private String sellerMobile;
    Order order;
    SellerBackup sellerBackup;
    BuyerBackup buyerBackup;
    OrderPay orderPay;
    List<ProductsBackup> productsBackupList;
    List<Order> subOrdersBackup;
    ProductsBackup product;

    public List<AgreementBackup> getAgreementBackup() {
        return agreementBackup;
    }

    public void setAgreementBackup(List<AgreementBackup> agreementBackup) {
        this.agreementBackup = agreementBackup;
    }

    List<AgreementBackup> agreementBackup;
    public OrderDetailModelData(Order order) {
        this.order = order;
        if (order != null) {
            sellerBackup = order.getSellerBackup();
            buyerBackup = order.getBuyerBackup();
            orderPay = order.getOrderPayBackup();
            productsBackupList = order.getProductsBackup();
            subOrdersBackup = order.getSubOrdersBackup();
            if (!ListUtil.isEmpty(productsBackupList)) {
                product = productsBackupList.get(0);
            }
            agreementBackup = order.getAgreementBackup();
        }
    }

    public String getSellerMobile() {
        if (sellerBackup != null) {
            sellerMobile = sellerBackup.getTel();
        }
        return sellerMobile;
    }


    public String getBuyerUserName() {
        if (buyerBackup != null) {
            buyerUserName = buyerBackup.getName();
        }
        return buyerUserName;
    }

    public void setBuyerUserName(String buyerUserName) {
        this.buyerUserName = buyerUserName;
    }

    public void setSellerMobile(String sellerMobile) {
        this.sellerMobile = sellerMobile;
    }

    public int getNum() {
        if (product != null) {
            num = product.getCount();
        }
        return num;
    }

    public String getBuyerMobile() {
        if (buyerBackup != null) {
            buyerMobile = buyerBackup.getMobile();
        }
        return buyerMobile;
    }

    public void setBuyerMobile(String buyerMobile) {
        this.buyerMobile = buyerMobile;
    }

    public void setNum(int num) {
        this.num = num;
    }

    private int num = 1;

    public String getUserName() {
        if (sellerBackup != null) {
            userName = sellerBackup.getName();
        }
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getMobile() {
        if (sellerBackup != null) {
            mobile = sellerBackup.getTel();
        }
        return TextUtils.isEmpty(mobile) ? "" : mobile;
    }

    public void setCode(String code) {

        this.code = code;
    }

    /**
     * 用于展示
     * @return
     */
    public String getCode() {
        if (order != null) {
            code = order.getCode();
        }
        return code;
    }

    /**
     * 对订单进行操作的时候用order.getId
     * 对订单展示用code
     * @return
     */
    public String getOrderId() {
        if (order != null) {
            return order.getId();
        }
        return "";
    }


    public void setGoodsType(String goodsType) {
        this.goodsType = goodsType;
    }

    public String getGoodsType() {
        if (!ListUtil.isEmpty(productsBackupList) && order.getProductsBackup().get(0) != null) {
            goodsType = productsBackupList.get(0).getSubject();
        }
        return goodsType;
    }

    public void setStatus(int status) {

        this.status = status;
    }

    public int getStatus() {
        if (order != null) {
            status = order.getStatus();
        }
        return status;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public String getCover() {
        if (!ListUtil.isEmpty(productsBackupList) && order.getProductsBackup().get(0) != null
                && !ListUtil.isEmpty(productsBackupList.get(0).getCover())) {
            cover = productsBackupList.get(0).getCover().get(0);
        }
        return cover;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        if (product != null) {
            title = product.getTitle();
        }
        return TextUtils.isEmpty(title) ? "" : title;
    }

    public void setCreateTime(int createTime) {
        this.createTime = createTime;
    }

    public long getCreateTime() {
        createTime  = order!= null ? order.getCreateTime():0;
        return createTime;
    }

    public void setPayTime(long payTime) {
        this.payTime = payTime;
    }

    public long getPayTime() {
        payTime  = order!= null  ? order.getPayTime():0;
        return payTime;
    }

    public void setPayMethod(int payMethod) {
        this.payMethod = payMethod;
    }

    public int getPayMethod() {
        payMethod = order!= null ? order.getPayMethod():0;
        return payMethod;
    }

    public String getPayMethodString() {
        if (getPayMethod() == 1 || getPayMethod() == 5) {
            return MyApp.getAppContext().getResources().getString(R.string.alipay);
        } else if (getPayMethod() == 2 || getPayMethod() == 6) {
            return MyApp.getAppContext().getResources().getString(R.string.wechat);
        }
        else if (getPayMethod() == 3) {
            return MyApp.getAppContext().getResources().getString(R.string.bank_card);
        }
        else if (getPayMethod() == 4) {
            return MyApp.getAppContext().getResources().getString(R.string.apple_pay);
        }
        return "";
    }

    public void setPayCode(String payCode) {
        this.payCode = payCode;
    }

    public String getPayCode() {
        payCode = order != null ? order.getPayCode() : "";
        return TextUtils.isEmpty(payCode) ? "" : payCode;
    }

    public void setSellerName(String sellerName) {
        this.sellerName = sellerName;
    }

    public String getSellerName() {
        if (sellerBackup != null) {
            sellerName = sellerBackup.getName();
        }
        return TextUtils.isEmpty(sellerName) ? "" : sellerName;
    }

    public void setSellerId(String sellerId) {
        this.sellerId = sellerId;
    }

    public String getSellerId() {
        if (sellerBackup != null) {
            sellerId = sellerBackup.getId();
        }
        return sellerId;
    }

    public void setSellerLevel(int sellerLevel) {
        this.sellerLevel = sellerLevel;
    }

    public int getSellerLevel() {
        if (sellerBackup != null) {
            sellerLevel = sellerBackup.getLevel();
        }
        return sellerLevel;
    }

    public void setSellerAvatar(String sellerAvatar) {
        this.sellerAvatar = sellerAvatar;
    }

    public String getSellerAvatar() {
        if (sellerBackup != null) {
            sellerAvatar = sellerBackup.getAvatar();
        }
        return TextUtils.isEmpty(sellerAvatar) ? "" : sellerAvatar;
    }

    public String getBuyerAvatar() {
        if (buyerBackup != null) {
            buyerAvatar = buyerBackup.getAvatar();
        }
        return TextUtils.isEmpty(buyerAvatar) ? "" : buyerAvatar;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public double getAmount() {
        if (order != null) {
            amount = order.getAmount();
        }
        return amount;
    }

    public void setPayAmount(float payAmount) {
        this.payAmount = payAmount;
    }

    public double getPayAmount() {
        if (order != null) {
            payAmount = order.getPayAmount();
        }
        return payAmount;
    }

    public void setVideoStages(int videoStages) {
        this.videoStages = videoStages;
    }

    public int getVideoStages() {
        if (product != null && product.getMediaSummary() != null) {
            videoStages = product.getMediaSummary().getCount();
        }
        return videoStages;
    }

    public int getClassCount() {
        int classCount = 0;
        if (order != null) {
            AgreementBackup agreementBackup = ListUtil.isEmpty(order.getAgreementBackup()) ? null : order.getAgreementBackup().get(0);
            if (!TextUtils.isEmpty(agreementBackup.getMoreBackup()) && !TextUtils.isEmpty(getGoodsType())) {
                if (getGoodsType().equals(Subject.training_course)) {
                    TrainingCourse trainingCourse = JsonUtil.toObject(agreementBackup.getMoreBackup(),TrainingCourse.class);
                    if (trainingCourse != null) {
                        classCount = trainingCourse.getClassCount();
                    }
                }
                else if (getGoodsType().equals(Subject.major_course)) {
                    MajorCourse majorCourse = JsonUtil.toObject(agreementBackup.getMoreBackup(),MajorCourse.class);
                    if (majorCourse != null) {
                        classCount = majorCourse.getClassCount();
                    }
                }
            }
        }
        return classCount;
    }


    public void setSource(String source) {
        this.source = source;
    }

    public String getSource() {
        if (order != null) {
            source = order.getSource();
        }
        return TextUtils.isEmpty(source) ? "" : source;
    }

    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    public String getSourceUrl() {
        if (order != null) {
            sourceUrl = order.getSourceUrl();
        }
        return TextUtils.isEmpty(sourceUrl) ? "" : sourceUrl;
    }

    public void setRouter(String router) {
        this.router = router;
    }

    public String getRouter() {
        if (!ListUtil.isEmpty(getAgreementBackup())) {
            router = getAgreementBackup().get(0).getRouter();
        }
        return TextUtils.isEmpty(router) ? "" : router;
    }

    public String getMomentId() {
        if (product != null) {
            return product.getMomentId();
        }
        return "";
    }


}
